<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class TestListRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
            case 'POST':
                return [
                    'name' => 'required|min:2|max:20|unique:test_lists,name',
                    'paper_id' => 'required|numeric',
                    'subject_id' => 'required|numeric',
                    'type' => 'required|numeric',
                    'status' => 'required|in:0,1',
                    'order' => 'required',
                    'random' => ['sometimes', function ($attr, $value, $fail) {
                        foreach ($value as $k => $item) {
                            if (!is_array($item)) {
                                $fail($k . '内的格式不正确');
                            }
                            foreach ($item as $v) {
                                if (!is_numeric($v['num']) || empty($v['num'])) {
                                    $fail('num 格式不正确');
                                }
                                if (!($v['level'] >= 1 && $v['level'] <= 3)) {
                                    $fail('难度等级必须在（1-3）中');
                                }
                                if (!is_numeric($v['category_id']) || empty($v['category_id'])) {
                                    $fail('category_id 格式不正确');
                                }
                            }
                        }
                    }],
                    'fixed' => ['sometimes', function ($attr, $value, $fail) {
                        if (!is_array($value)) {
                            $fail('格式不正确');
                        }
                        if (empty($value)) {
                            $fail('数据不能为空');
                        }
                    }],
                    'artificial' => 'required|in:0,1'
                ];
            case 'PUT':
                return [
//                    'name' => ['sometimes', 'min:2', 'max:20', 'unique:test_lists,name,' . $this->route()->parameter('testList')->id],
                    'paper_id' => 'sometimes|numeric',
                    'subject_id' => 'sometimes|numeric',
                    'type' => 'sometimes|numeric',
                    'status' => 'sometimes|in:0,1',
                    'order' => 'sometimes',
                    'random' => ['sometimes', function ($attr, $value, $fail) {
                        foreach ($value as $k => $item) {
                            if (!is_array($item)) {
                                $fail($k . '内的格式不正确');
                            }
                            foreach ($item as $v) {
                                if (!is_numeric($v['num'])) {
                                    $fail('num 格式不正确');
                                }
                                if (!($v['level'] >= 0 && $v['level'] <= 3)) {
                                    $fail('难度等级必须在（1-3）中,或不限星');
                                }
                                if (!is_numeric($v['category_id'])) {
                                    $fail('category_id 格式不正确');
                                }
                            }
                        }
                    }],
                    'fixed' => ['sometimes', function ($attr, $value, $fail) {
                        if (!is_array($value)) {
                            $fail('格式不正确');
                        }
                        if (empty($value)) {
                            $fail('数据不能为空');
                        }
                    }],
                    'artificial' => 'sometimes|in:0,1'
                ];
        }

    }
}
